package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.romotc.qqrobot.config.QQUrlWatcherConfigService;
import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;
import com.romotc.qqrobot.webqq.qqInterfaceLog.QQUrlLogFactory;
import com.romotc.qqrobot.webqq.qqInterfaceLog.QQUrlLogInterface;
import com.romotc.scache.mgr;

public class QQUrlCatchingFactory {

	public static List<QQUrlWatcherConfig> urlConfigs;
	static {
		urlConfigs = QQUrlWatcherConfigService.getAllConfigs();
	}
	
	private static Map<String, QQUrlCatchingInterface> clazzMap = new HashMap<String, QQUrlCatchingInterface>();;
	
	public static synchronized QQUrlCatchingInterface getCating(String url) {
		QQUrlWatcherConfig config = QQUrlWatcherConfigService.getConfigByUrl(url, urlConfigs);
		if(config == null || "*".equals(config.getUrl()))
			return null;
		if(config.getCatchingClass() == null 
				|| "".equals(config.getCatchingClass().trim())
				|| config.getCatchingClass().startsWith("#"))
		{
			config.setCatchingClass("com.romotc.qqrobot.webqq.qqInterfaceCatching.DefaultUrl_Catching");
			//romUnDo 设置为默认的类后去掉return null
//			return null;
		}
		
		QQUrlCatchingInterface catching = clazzMap.get(config.getCatchingClass());
		if(catching == null) {
			try {
				catching = (QQUrlCatchingInterface) Class.forName(config.getCatchingClass()).newInstance();
				clazzMap.put(config.getCatchingClass(), catching);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return catching;
	}

	/**
	 * 获取指定url的catching列表
	 * @param url
	 * @return
	 */
	public static List<QQUrlCatchingInterface> getCatingList(String url) {
		
		boolean defautlLoged = false;//已经使用默认日志记录过
		
		List<QQUrlCatchingInterface> list = new ArrayList<QQUrlCatchingInterface>();
		for(QQUrlWatcherConfig config : urlConfigs) {
			if(config.getUrl() == null || "".equals(config.getUrl())) {
				continue;
			}
			
			if(mgr.simpleWildMatch(config.getUrl(), url) != null) {
				try {
					if(config.getCatchingClass() == null 
							|| "".equals(config.getCatchingClass().trim())
							|| config.getCatchingClass().startsWith("#")) {
						config.setCatchingClass("com.romotc.qqrobot.webqq.qqInterfaceCatching.DefaultUrl_Catching");
						if(defautlLoged)
							continue;
						else
							defautlLoged = true;
					}
					
					QQUrlCatchingInterface catching = clazzMap.get(config.getCatchingClass());
					if(catching == null) {
						try {
							catching = (QQUrlCatchingInterface) Class.forName(config.getCatchingClass()).newInstance();
							clazzMap.put(config.getCatchingClass(), catching);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					list.add(catching);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		return list;
	}

}
